#include<bits/stdc++.h>
using namespace std;
#define int long long
int poww[18]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072};
int nu(int x){
	int ans=0;
	while(x) x/=2,ans++;
	return ans;
}
int a[5005],c[5005],aa[5005];
char d[25][2505];
int winner(int cs,int st,int ed){
	if(cs==1) {
		if(d[1][ed/poww[cs]]=='0'){
			if(aa[st]>=cs){
				return st;
			} 
			else{
				return ed;
			}
		}
		else if(aa[ed]>=cs){
			return ed;
		}
		else{
			return st;
		}
	}
	int x=winner(cs-1,st,st+poww[cs]-1),y=winner(cs-1,st+poww[cs],ed);
	if(d[cs][y/poww[cs]]=='0'){
		if(aa[x]>=cs){
			return x;
		} 
		else{
			return y;
		}
	}
	else if(aa[y]>=cs){
		return y;
	}
	else{
		return x;
	}
}
signed main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	int n,m;cin>>n>>m;
	//cout<<n<<' '<<m<<endl;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		//cout<<a[i]<<' ';
	}
	//cout<<endl;
	for(int i=1;i<=m;i++){
		cin>>c[i];
		//cout<<c[i]<<' ';
	}
	//cout<<endl;
	cout<<nu(n)<<' ';
	if(poww[nu(n)]==n) 
		for(int i=1;i<=nu(n);i++){
			for(int j=1;j<=n/poww[i];j++){
				cin>>d[i][j];
				//cout<<d[i][j]<<' ';
			}
			//cout<<endl;
		}
	else{
		for(int i=1;i<=nu(n);i++){
			for(int j=1;j<=poww[nu(n)-i];j++){
				cin>>d[i][j];
				//cout<<d[i][j]<<' ';
			}
			//cout<<endl;
		}
	}
	int t;cin>>t;
	//cout<<t<<endl;
	while(t--){
		int x0,x1,x2,x3,ans=0;cin>>x0>>x1>>x2>>x3;
		//cout<<x0<<' '<<x1<<' '<<x2<<' '<<x3<<endl;
		for(int i=1;i<=n;i++){
			if(i%4==0)
				aa[i]=a[i]^x0;
			if(i%4==1)
				aa[i]=a[i]^x1;
			if(i%4==2)
				aa[i]=a[i]^x2;
			if(i%4==3)
				aa[i]=a[i]^x3;
		}
		for(int i=1;i<=m;i++){
			if(poww[nu(i)]==i){
				ans=ans^(i*winner(nu(i),1,c[i]));
			}
		}
		//cout<<ans<<endl;
	}
}
